import React, {Component} from 'react';
import {Button, Card, Avatar, List, Badge, Radio, message} from 'antd'
import { connect} from 'react-redux'
import './style.less'
import {passOrder, passAllOrder, deleteOrder} from "../../api/order";
import {getNotifications} from './../../actions/notification'


const mapStateToProps = (state,props) => {
    return {
        notification: state.notification
    }
}

const mapDispatchToProps = (dispatch,props) => {
    return {
        getNotification: () => {
            getNotifications(dispatch)
        }
    }
}

@connect(mapStateToProps,mapDispatchToProps)

class Notification extends Component {
    constructor(props){
        super(props);
        this.state = {
        }
    }

    //全部审核通过
    passAll= () => {
        passAllOrder().then(res => {
            message.success(res.msg)
            this.props.getNotification();
        }).catch(err => {
            message.error(err)
        })
    }

    //审核通过
    pass = (item) => {
        passOrder(item).then(res => {
            message.success(res.msg)
            this.props.getNotification();
        }).catch(err => {
            message.error(err)
        })
    }

    //审核拒绝
    delete = (item) => {
        deleteOrder(item).then(res => {
            message.success(res.msg)
            this.props.getNotification();
        }).catch(err => {
            message.error(err)
        })
    }

    componentDidMount() {
    }

    render() {
        return (
            <Card title='审核中心'  extra={<Button onClick={this.passAll}>全部审核通过</Button>}>
                <List
                    itemLayout="horizontal"
                    dataSource={this.props.notification}
                    renderItem={(item,index) => {
                        return <List.Item key={index}>
                            <Badge dot={item.isPass === false}>
                                <List.Item.Meta
                                    title={<span>用户编号：  {item.userId}</span>}
                                    description={<div><span className='listSpan'>汽车编号：{item.carId}</span><span className='listSpan'>租车时间：{item.carId}</span><span>租金：{item.money}</span></div>}
                                />
                            </Badge>
                            {
                                <Radio.Group>
                                    <Button onClick={this.pass.bind(this,item)} disabled={item.isPass}>{item.isPass ? '已审核通过': '审核通过'}</Button>
                                    <Button onClick={this.delete.bind(this,item)}>{item.isPass ? '删除': '审核拒绝'}</Button>
                                </Radio.Group>
                            }
                        </List.Item>
                    }}
                />
            </Card>
        );
    }
}

export default Notification;